﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

namespace FileDB
{
    public partial class Customer
    {
        public static string RootDir = "Customers";
        public static string FullRootDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DataManager.DataDir, RootDir);
        private static string MetaDataFileName = "00000000.xml";
        private static string MetaDataFullFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), DataManager.DataDir, RootDir, MetaDataFileName);
        public const string MaxId = "MaxId";
        public const string SelfCompany = "SelfCompany";

        internal static void CreateDefaultTemplate()
        {
            if (File.Exists(MetaDataFullFileName))
                throw new Exception(string.Format("MetaData file already exists", MetaDataFullFileName));

            XElement metadata =
                new XElement("MetaData",
                    new XAttribute(MaxId, 0.ToString("d8")),
                    new XAttribute(SelfCompany, 0.ToString("d8")));
            metadata.Save(MetaDataFullFileName);
        }

        internal static int NextID()
        {
            XDocument xml = XDocument.Load(MetaDataFullFileName);
            return Int32.Parse(xml.Root.Attribute(MaxId).Value) + 1;
        }

        internal static void UpdateMaxId(int nextId)
        {
            XDocument xml = XDocument.Load(MetaDataFullFileName);
            xml.Root.Attribute(MaxId).SetValue(nextId.ToString("d8"));
            xml.Save(MetaDataFullFileName);
        }

        public static string GetFileName(Customer c)
        {
            return GetFileName(c.Id);
        }
        public static string GetFileName(int Id)
        {
            return Path.Combine(FullRootDir,
                string.Format("{0}.xml",
                Id.ToString("d8")));
        }

        public static int Insert(Customer customer)
        {
            int id = NextID();
            customer.Id = id;
            Save(customer, GetFileName(id));
            UpdateMaxId(id);
            return id;
        }

        public static Customer GetData(string fileName)
        {
            XDocument xml = XDocument.Load(fileName);
            var reminder = xml.Root;
            return new Customer()
            {
                Id = Int32.Parse(reminder.Attribute("Id").Value),
                CardId = reminder.Element("CardId").Value,
                Comment = reminder.Element("Comment").Value,
                Name = reminder.Element("Name").Value,
                Phone = reminder.Element("Phone").Value,
                FileName = fileName,

            };
        }
        private static void Save(Customer customer, string fileName)
        {
            XElement r =
                new XElement("Customer", new XAttribute("Id", customer.Id),
                    new XElement("CardId", customer.CardId),
                    new XElement("Comment", customer.Comment),
                    new XElement("Name", customer.Name),
                    new XElement("Phone", customer.Phone));
            customer.FileName = fileName;
            r.Save(fileName);
        }


        public static List<Customer> GetAllCustomer()
        {
            List<Customer> customers = new List<Customer>();
            var fileList = Directory.GetFiles(FullRootDir);

            foreach (var fileName in fileList)
            {
                if (fileName != MetaDataFullFileName)
                    customers.Add(GetData(fileName));
            }
            return customers;
        }

        public bool SetAsSelfCompany()
        {
            if (this.Id > 0)
            {
                XDocument xml = XDocument.Load(MetaDataFullFileName);
                xml.Root.Attribute(SelfCompany).SetValue(this.Id.ToString("d8"));
                xml.Save(MetaDataFullFileName);
                return true;
            }
            return false;
        }

        public static Customer GetSelfCompany()
        {
            XDocument xml = XDocument.Load(MetaDataFullFileName);
            int id = Int32.Parse(xml.Root.Attribute(SelfCompany).Value);
            if (id > 0)
            {
                return GetCustomer(id);
            }
            else
            {
                return null;
            }
        }
        public static void PrepareData(Customer c)
        {
            var customer = GetSelfCompany();
            if (customer == null || customer.Id < 1)
            {
                c.Save();
                c.SetAsSelfCompany();
            }
        }
    }
    public partial class Customer : IFileDB
    {
        private string _fileName;

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }

    }
}
